/**
 * 数据列表组件
 */
import React, { Component ,Fragment} from 'react';
import {List} from 'antd';

import { ajax,isNull} from '../common';

export default class UIDataList extends Component{
	state = {
		records : [],
		params : {},
		pagination: {
			current : 1,
			defaultCurrent : 1,
			pageSize : isNull(this.props.pageSize) ? 10 : this.props.pageSize,
			showSizeChanger : true,
			onChange: ((page, pageSize) => {
				console.log('onChange');
			  	this.fetchData(this.state.params,{'current' : page,'pageSize':pageSize});
		  	}),
		  	onShowSizeChange : ((page, pageSize) => {
		  		console.log('onShowSizeChange');
			  	this.fetchData(this.state.params,{'current' : page,'pageSize':pageSize});
		  	}),
		 },
		loading : false,
	}
	
	componentDidMount() {
		if(this.props.autoLoad){
			this.loadData(this.props.params);
		}
	}

	setSelectedRowKeys =(rowKeys) => {

	}
	
	getListData = () => {
		return this.state.records;
	}
	
	fetchData = (params,pageParams) => {
		const owner = this;
		const pagination = { ...this.state.pagination };
		this.setState({loading : true});
		ajax(this.props.dataUrl,{...params,offset : ((pageParams.current-1) * pageParams.pageSize),limit : pageParams.pageSize }).then((data) => {
      		pagination.total = data.total;
      		pagination.current = pageParams.current;
      		pagination.pageSize = data.pageCount;
      		owner.setState({
      			params : params,
        			loading: false,
        			records: data.records,
        			pagination
      		});
		});
	}
	
	loadData = (params) =>{
		//this.setState(params);
		this.fetchData(params,this.state.pagination);
	}

	 searchData = (params = {}) => {
        //this.setState({ params: params });
        this.loadData({ ...params });
    }
	
	render() {
		const {records,pagination} = this.state;
		
		return (
			<Fragment>
			<List
		        itemLayout={this.props.itemLayout}
		        grid={this.props.grid}
		        header={this.props.header}
				pagination={pagination}
				dataSource={records}
		        renderItem={listItem => {
		        		if(this.props.renderListItem !== undefined){
		        			return this.props.renderListItem(listItem);
		        		}
		        		else{
		        			return <List.Item>{listItem}</List.Item>
		        		}
		        	}}
			/>
			</Fragment>
		);
	}
}
